//
//  XMLLanguageParser.m
//  LanguagesTeach
//
//  Created by Niel Santos on 2/9/13.
//  Copyright 2013 __MyCompanyName__. All rights reserved.
//

#import "XMLLanguageParser.h"
#import "Level.h"
#import "MetaUnit.h"


@implementation XMLLanguageParser

@synthesize levels;

- (void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qualifiedName
	attributes:(NSDictionary *)attributeDict{
	
	MetaUnit *metaUnit = nil;
	Level *level = nil;
	
	if([elementName isEqualToString:@"levels"]) {
		//Treat levels
		levels = [[NSMutableArray alloc] init];
	}else if([elementName isEqualToString:@"level"]) {
		level = [[Level alloc] init];
		level.title = [attributeDict objectForKey:@"title"];
		level.folder = [attributeDict objectForKey:@"folder"];
		[levels addObject: level];
		[level release];
	}else if([elementName isEqualToString:@"unit"]){
		metaUnit = [[MetaUnit alloc] init];
		metaUnit.title = [attributeDict objectForKey:@"title"];
		metaUnit.subFolder = [attributeDict objectForKey:@"sub_folder"];
		[[[levels lastObject] metaUnits] addObject: metaUnit];
		[metaUnit release];
	}
}

- (void)dealloc {
	[levels release];
    [super dealloc];
}

@end
